package service.impl;

import entity.Message;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import service.FooService;

/**
 * Created by wang.bh
 */
public class DefaultFooService implements FooService {

    private DriverManagerDataSource dataSource;
    private InnerService innerService;

    @Override
    public Message getMessage(String fooName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Message getMessage(String fooName, String barName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void insertFoo(Message msg) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void updateFoo(Message msg) {
        JdbcTemplate template = new JdbcTemplate(dataSource);
        template.update("update USER_INFO set age = age + 20 where UNAME = 'tangmaru'");

        //throw new RuntimeException("in failed");
        //System.out.println("in success!");
    }

    @Override
    public void require() {
        JdbcTemplate template = new JdbcTemplate(dataSource);

        try {
            // Trans B
            innerService.updateFoo(new Message());
        } catch (Exception e) {
            System.out.println("update failed: " + e.getMessage());
        }

        template.update("update USER_INFO set age = age + 10");
        //insertFoo(new Message());
        System.out.println("out success!");
    }

    public void setDataSource(DriverManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setInnerService(InnerService innerService) {
        this.innerService = innerService;
    }
}
